package com.idanchuang.count.core.util.keyformat;

/**
 * 通用计数场景（简单计数器）相关key
 * @author Richard_yyf
 * @version 1.0 2021/9/23
 */
public class SimpleCountKeysFormatter {

    /**
     * 唯一ID
     * 幂等用
     * "cs:uid#bizId:id"
     * 为了支持 hashTag 改成："cs:uid{#bizId:#bizNo}:#time"
     * 简单计数场景：每个time字段下只会有一条记录产生，uid可以用cs:uid{#bizId:#bizNo}:#time 表示！
     * https://access.feishu.cn/wiki/wikcnZxHnUKnSsPYt48WTvygie6
     */
    private static final String UID_KEY_FORMAT = "cs:uid{%s:%s}:%s";

    /**
     * 简单计数器场景 key 格式
     * count:{bizCode.id}:{bizNo}
     * 为了支持 hashTag 改成："count{#bizId:#bizNo}"
     */
    private static final String COUNT_KEY_FORMAT = "count{%s:%s}";

    /**
     * 生成uuid key
     * @param bizId bizId
     * @param time 秒级时间戳
     * @return uuidKey
     */
    public static String formatUidKey(Integer bizId, String bizNo, String time) {
        // "cs:uid{#bizId:#bizNo}:#time"
        return String.format(UID_KEY_FORMAT, bizId, bizNo, time);
    }

    public static String formatCountKey(Integer bizId, String bizNo) {
        return String.format(COUNT_KEY_FORMAT, bizId, bizNo);
    }
}
